package com.cjw.campus.system.controller;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.BackgroundUserTable;
import com.cjw.campus.system.service.IBackgroundUserTableService;
import core.result.JsonResult;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-11-11
 */
@RestController
@RequestMapping("/system/backgroundUserTable")
@Api(tags = "用户管理", value = "用户管理接口")
public class BackgroundUserTableController {

    @Autowired
    private IBackgroundUserTableService backgroundUserTableService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有用户信息", notes = "查询所有用户信息", httpMethod = "GET")
    public JsonResult list () {
        List<BackgroundUserTable> list = backgroundUserTableService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息", httpMethod = "GET")
    @ApiParam(name = "backgroundUserId", value = "用户ID", required = true)
    public JsonResult getById (Integer backgroundUserId) {
        BackgroundUserTable backgroundUser = backgroundUserTableService.getById(backgroundUserId);
        return JsonResult.ok(backgroundUser);
    }

    @GetMapping("/getByBackgroundUserPage")
    @ApiOperation(value = "模糊分页查询用户信息", notes = "模糊分页查询用户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "backgroundUserName", value = "用户姓名", dataType = "String"),
            @ApiImplicitParam(name = "backgroundUserAccount", value = "用户账号", dataType = "String"),
    })
    public JsonResult getByBackgroundUserPage (Integer currentPage,Integer pageSize,String backgroundUserName, String backgroundUserAccount) {
        IPage<BackgroundUserTable> page = new Page<>(currentPage, pageSize);
        // QueryWrapper MybatisPlus 条件构造器
        QueryWrapper<BackgroundUserTable> queryWrapper = new QueryWrapper<>();
        // lambda 表达式
        queryWrapper.lambda().like(BackgroundUserTable::getBackgroundUserName, backgroundUserName);
        queryWrapper.lambda().like(BackgroundUserTable::getBackgroundUserAccount, backgroundUserAccount);
        IPage<BackgroundUserTable> mapIPage = backgroundUserTableService.page(page, queryWrapper);
        return JsonResult.ok(mapIPage);
    }

    @GetMapping("/getBackgroundUserPage")
    @ApiOperation(value = "分页查询用户信息", notes = "分页查询用户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2")
    })
    public JsonResult getBackgroundUserPage (Integer currentPage,Integer pageSize) {
        IPage<BackgroundUserTable> page = new Page<>(currentPage, pageSize);
        IPage<BackgroundUserTable> BackgroundUserPage = backgroundUserTableService.page(page);
        return JsonResult.ok(BackgroundUserPage);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户信息", notes = "保存用户信息", httpMethod = "POST")
    @ApiParam(name = "backgroundUser", value = "用户实体类", required = true)
    public JsonResult save (@RequestBody BackgroundUserTable backgroundUser) {
        // 保存用户信息
        boolean save = backgroundUserTableService.save(backgroundUser);
        // 判断用户是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新用户信息", notes = "根据id更新用户信息", httpMethod = "POST")
    @ApiParam(name = "backgroundUser", value = "用户实体类", required = true)
    public JsonResult updateById (@RequestBody BackgroundUserTable backgroundUser) {
        boolean updateById = backgroundUserTableService.updateById(backgroundUser);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除用户信息", notes = "根据id删除用户信息", httpMethod = "GET")
    @ApiParam(name = "backgroundUserId", value = "用户ID", required = true)
    public JsonResult removeById (Long backgroundUserId) {
        boolean removeBackgroundUserId = backgroundUserTableService.removeById(backgroundUserId);
//         判断用户是否删除成功
        if (removeBackgroundUserId) {
            // 成功返回
            return JsonResult.ok("删除成功");
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除用户信息", notes = "根据id批量删除用户信息", httpMethod = "POST")
    @ApiParam(name = "backgroundUserId", value = "用户ID", required = true)
    public JsonResult removeByIds (@RequestBody Long[] idList) {
        boolean removeBackgroundUserIds = backgroundUserTableService.removeByIds(Arrays.asList(idList));
        if (removeBackgroundUserIds) {
            // 成功返回
            return JsonResult.ok("删除成功");
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping("/getByBackgroundUser")
    @ApiOperation(value = "登录账号密码", notes = "登录账号密码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "backgroundUserAccount", value = "用户账号", dataType = "String"),
            @ApiImplicitParam(name = "backgroundUserPassword", value = "用户密码", dataType = "String")
    })
    public JsonResult getByBackgroundUser (String backgroundUserAccount, String backgroundUserPassword) {
        // QueryWrapper MybatisPlus 条件构造器
        QueryWrapper<BackgroundUserTable> queryWrapper = new QueryWrapper<>();
        // lambda 表达式
        queryWrapper.lambda().eq(BackgroundUserTable::getBackgroundUserAccount, backgroundUserAccount);
        queryWrapper.lambda().eq(BackgroundUserTable::getBackgroundUserPassword, backgroundUserPassword);
        BackgroundUserTable mapBackground = backgroundUserTableService.getOne(queryWrapper);
        if (mapBackground == null) {
            return JsonResult.error();
        }

        // 执行登录功能
        // 签发令牌 =》 JWT   JSON WEB Token
        // 先将数据存放到map中
        Map<String, Object> map = new HashMap<>();
        map.put("foregroundUser", mapBackground);
        // 当前时间
        map.put("currentTime", System.currentTimeMillis());
//            生成token
        String token = JWTUtil.createToken(map, "1234".getBytes());
        map.put("token", token);
        System.out.println(token);

        return JsonResult.ok(map);
    }

}
